﻿using Microsoft.Ajax.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Web.Mvc;

namespace 软件工程实验.Controllers
{
    public class TeacherController : Controller
    {
        public 成绩表 t成绩 = new 成绩表();
        public 测试表 t测试 = new 测试表();
        public 习题表 t习题 = new 习题表();
        // GET: Teacher
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult Create() 
        {
            ViewBag.ResultMessage = string.Empty;
            var tGrade = new 成绩表();
            return View(tGrade);
        }
        //新建学生信息
        [HttpPost]
        public ActionResult Create(成绩表 model)
        {
            DBHelper.DB.成绩表.Add(model);
            DBHelper.DB.测试表.Add(new 测试表() { 学号 = model.学号 });
            DBHelper.DB.习题表.Add(new 习题表() { 学号 = model.学号 });
            DBHelper.Save();
            ViewBag.ResultMessage = $"添加成功: {model.学号} {model.姓名}";
            return View();
        }

        [HttpGet]
        public ActionResult Modify()
        {
            ViewBag.ErrorMessage = string.Empty;
            GradeModel tGrade = new GradeModel();
            tGrade.成绩 = new 成绩表();
            tGrade.测试 = new 测试表();
            tGrade.习题 = new 习题表();
            return View(tGrade);
        }
        [HttpPost]
        public ActionResult Modify(GradeModel stu)
        {
            var temp = DBHelper.DB.成绩表.Where(o => o.学号.Equals(stu.成绩.学号)).FirstOrDefault();
            if (temp == null)
            {
                ViewBag.ResultMessage = $"修改失败,未找到: {stu.成绩.学号} {stu.成绩.姓名}";
            }
            else
            {
                var temp1 = DBHelper.DB.习题表.Where(o => o.学号.Equals(stu.成绩.学号)).FirstOrDefault();
                var temp2 = DBHelper.DB.测试表.Where(o => o.学号.Equals(stu.成绩.学号)).FirstOrDefault();
                temp1.习题1 = stu.习题.习题1;
                temp1.习题2 = stu.习题.习题2;
                temp1.习题3 = stu.习题.习题3;
                temp1.习题4 = stu.习题.习题4;
                temp1.习题5 = stu.习题.习题5;
                temp1.习题6 = stu.习题.习题6;
                temp1.习题7 = stu.习题.习题7;
                temp1.习题8 = stu.习题.习题8;
                temp1.习题9 = stu.习题.习题9;
                temp1.习题10 = stu.习题.习题10;
                temp1.习题11 = stu.习题.习题11;
                temp1.习题12 = stu.习题.习题12;
                temp1.习题13 = stu.习题.习题13;
                temp1.习题14 = stu.习题.习题14;
                temp1.习题15 = stu.习题.习题15;
                temp1.习题16 = stu.习题.习题16;
                temp1.平均分 = (temp1.习题1 + temp1.习题2 + temp1.习题3 + temp1.习题4 + temp1.习题5 + temp1.习题6 + temp1.习题7 + temp1.习题8 + temp1.习题9 + temp1.习题10 + temp1.习题11 + temp1.习题12 + temp1.习题13 + temp1.习题14 + temp1.习题15 + temp1.习题16) / 16;
                temp2.测试1 = stu.测试.测试1;
                temp2.测试2 = stu.测试.测试2;
                temp2.测试3 = stu.测试.测试3;
                temp2.平均分 = (temp2.测试1 + temp2.测试2 + temp2.测试3) / 3;
                temp.考试成绩 = stu.成绩.考试成绩;
                temp.总成绩 = temp1.平均分 * 0.1 + temp2.平均分 * 0.2 + temp.考试成绩 * 0.7;

                DBHelper.Save();
                ViewBag.ResultMessage = $"修改成功: {stu.成绩.学号} {stu.成绩.姓名}";
            }
            return View();
        }
        [HttpGet]
        public ActionResult Delete()
        {

            return View();
        }
        [HttpPost]
        public ActionResult Delete(成绩表 stu) 
        {
            
            var temp1 = DBHelper.DB.成绩表.Where(o => o.学号.Equals(stu.学号) && o.姓名.Equals(stu.姓名)).ToList();
            if (temp1.Count == 0)
            {
                ViewBag.ResultMessage = $"删除失败,未找到: {stu.学号} {stu.姓名}";
            }
            else
            {
                DBHelper.DB.成绩表.RemoveRange(temp1);
                var temp2 = DBHelper.DB.习题表.Where(o => o.学号.Equals(stu.学号)).ToList();
                DBHelper.DB.习题表.RemoveRange(temp2);
                var temp3 = DBHelper.DB.测试表.Where(o => o.学号.Equals(stu.学号)).ToList();
                DBHelper.DB.测试表.RemoveRange(temp3);
                DBHelper.Save();
                ViewBag.ResultMessage = $"删除成功: {stu.学号} {stu.姓名}";
            }
            return View(stu);
        }
    }
}